/**
 * 
 */
package com.vision.admin.cl.ui;

import java.util.List;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.vision.core.cl.mvp.Presenter;
import com.vision.core.cl.ui.IConfirmDialogHandler;
import com.vision.core.cl.ui.ITabItemPanel;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.View;

/**
 * @author Mark
 *
 */
public abstract class AbstractView<T> extends SimpleContainer implements View<T>, Editor<T>, ITabItemPanel {

	// Fields %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	protected Presenter<T> presenter;
	
	protected UiBinder<Widget, ? extends AbstractView<T>> binder;
	
	protected SimpleBeanEditorDriver<T, ? extends AbstractView<T>> driver;
	
	protected TabItemConfig config;
	
	// Constructor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@SuppressWarnings("unchecked")
	public
		<
			A extends AbstractView<T>,
			B extends UiBinder<Widget, A>,
			D extends SimpleBeanEditorDriver<T, A>
		> 
		AbstractView(B binder, D  driver) {
			this.binder = binder;
			this.driver = driver;
			
			preBinderInvoke();
			
			setWidget(binder.createAndBindUi((A) this));
			
			driver.initialize((A) this);
			driver.edit(null); // Edit a null
	}
	
	// Abstract Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	protected abstract void preBinderInvoke();
	
	protected abstract Grid<T> getGrid();
	
	// View Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public void mask(String message) {
		super.mask(message);
	}
	
	@Override
	public void unmask() {
		super.unmask();
	}
	
	@Override
	public void setPresenter(Presenter<T> presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setData(List<T> list) {
		getGrid().getStore().clear();
		getGrid().getStore().addAll(list);
	}

	@Override
	public T getSelected() {
		return getGrid().getSelectionModel().getSelectedItem();
	}

	@Override
	public void update(T obj) {
		getGrid().getStore().update(obj);
	}

	@Override
	public void remove(T obj) {
		getGrid().getStore().remove(obj);
	}

	@Override
	public void edit(T obj) {
		driver.edit(obj);
	}

	@Override
	public T getInEditor() {
		return driver.flush();
	}

	@Override
	public boolean isDirty() {
		return driver.isDirty();
	}

	@Override
	public boolean isFormValid() {
		return UI.isFormValid(driver);
	}

	@Override
	public void confirm(String header, String message,
			IConfirmDialogHandler handler) {
		UI.confirm(header, message, handler);
	}
	
	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public void loadData() {
		presenter.loadData();
	}
	
	@Override
	public String getBusyMessage() {
		return isDirty() ? "There are unsaved changes. Are you sure you want to navigate away?" : null;
	}
	
	// Helpers %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public TabItemConfig getConfig(String title, boolean closable, ImageResource icon) {
		if (config == null) {
			config = new TabItemConfig(title, closable);
			config.setIcon(icon);
		}
		return config;
	}
	
}
